# code to read in some monthly predictor variable series.
# raw data files are in my Z drive on school computer,
# so I will change to that directory first.

# this is from Bureau of Labor Statistics ... 
# macrotrends only had inflation rate data from once per year (only in the monthly rate in December)
rawdata_infl <- read.table("monthly_inflation_rate.txt", header=T)

#only need last column for the annual inflation rates:
inflation_yr_extra <- ts(rawdata_infl$AVE,start=1914,end=2021,frequency=1)
inflation_yr_1974_2016 <- ts(inflation_yr_extra[61:103],start=1974,end=2016,frequency=1)

inflation_mo_extra <- ts(as.vector(t(as.matrix(rawdata_infl[2:13]))), start=c(1914,1), end=c(2021,12), frequency=12)

# Just picking out the months between 1974 and 2016 in the time series:
inflation_mo_1974_2016 <- ts(inflation_mo_extra[721:1236],start=c(1974,1), end=c(2016,12), frequency=12)

# This is the same macrotrends data we originally used...
rawdata_unemp <- read.table("monthly_unemployment_rate.txt", header=T)

# This gets the 1974-2016 annual unemployment (averaging over months)
# Same as we originally used!
unemp_annual<-rowMeans(rawdata_unemp[27:69,-1])

unemployment_mo_extra <- ts(as.vector(t(as.matrix(rawdata_unemp[2:13]))), start=c(1948,1), end=c(2021,12), frequency=12)

# Just picking out the months between 1974 and 2016 in the time series:
unemployment_mo_1974_2016 <- ts(unemployment_mo_extra[313:888],start=c(1974,1), end=c(2016,12), frequency=12)

# annual political variable:

GSSpolitical<-read.table("annual_political_variable.txt",header=T)

calc_mean_from_counts <- function(vec,weights=seq(1,9,by=1)){
mymean<-sum(weights*(vec/sum(vec)))
return(mymean)
}
polit<-apply(GSSpolitical,2,FUN=calc_mean_from_counts)
years_polit <- c(1974:1978,1980,1982:1991,1993,seq(1994,2018,by=2))
#plot(years_polit,polit,type='o')

# Making it a yearly time series:
# Interpolating 
annual_polit<-approx(x=years_polit,y=polit,xout=1974:2016)

polit_pred <- annual_polit$y
polit_yrs <- annual_polit$x  # this is just 1974:2016

# Getting the annual GDP growth for years 1974 through 2016:
gdp.df <- read.table("gdp_growth.txt",header=T)
gdp_growth<-gdp.df$gdp_growth
 gdp_growth_1974_2016<-gdp_growth[14:56]
